package com.study.zzedu.fragment.home.qualityedu;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.study.zzedu.R;
import com.study.zzedu.adapter.QualityLessonAdapter;
import com.study.zzedu.base.BaseFragment;
import com.study.zzedu.base.BaseView;
import com.study.zzedu.entity.NoticeItem;
import com.study.zzedu.entity.QualityLessonItem;
import com.study.zzedu.ui.noticeinfodetail.NoticeInfoDetailActivity;
import com.study.zzedu.utils.AppConstant;
import com.study.zzedu.utils.ToastUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by kiros on 2020/9/5.
 * 课程公告页面
 */

public class QualityEduFragment extends BaseFragment<BaseView, QualityEduPresenter> implements BaseView{

    private RecyclerView rl_notice;
    private RecyclerView.LayoutManager layoutManager;
    private QualityLessonAdapter noticeAdapter;
    private List<QualityLessonItem> items = new ArrayList<>();

    @Override
    protected QualityEduPresenter createPresenter() {
        return new QualityEduPresenter();
    }

    @Override
    protected BaseView createView() {
        return this;
    }

    @Override
    protected int getLayoutID() {
        return R.layout.fragment_quality;
    }
    @Override
    protected void initView(View view) {
        rl_notice = view.findViewById(R.id.rc_quality);
        //view.findViewById(R.id.banner1).setVisibility(View.GONE);
    }

    @Override
    protected void initData() {
        layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        noticeAdapter = new QualityLessonAdapter(getContext(), items);

        rl_notice.setLayoutManager(layoutManager);
        rl_notice.setAdapter(noticeAdapter);

        getPresenter().queryMyQualityEducationList(getContext());
    }

    @Override
    protected void initListener() {
    }

    @Override
    public void getData(String data) {
        try {
            JSONObject jObject = new JSONObject(data);
            JSONArray jArray = jObject.getJSONArray("data");
            for (int i = 0;i<jArray.length();i++){
                items.add(new QualityLessonItem(
                        jArray.getJSONObject(i).getString("img"),
                        jArray.getJSONObject(i).getString("courseName"),
                                jArray.getJSONObject(i).getString("curTerm"),
                                jArray.getJSONObject(i).getString("courseCode"),
                        jArray.getJSONObject(i).getString("enterYear"),
                        (jArray.getJSONObject(i).getInt("totalHour")),
                        (jArray.getJSONObject(i).getString("type"))
                        ));
            }
            if(!(jArray.length()>0)){
                //ToastUtil.showShortToast("素质教育无数据");
                return;
            }else {
                getActivity().findViewById(R.id.rl_default_quality).setVisibility(View.GONE);
                noticeAdapter.notifyDataSetChanged();
            }
        } catch (Exception e) {
            e.toString();
        }
    }

    @Override
    public void showError(String error) {
        if (null != error && !TextUtils.isEmpty(error)) {
            ToastUtil.showShortToast(error);
        }
    }
}
